#    Supsta
#    Copyright (c) 2014 Sebastian Dufner
#
#    This file is part of Supsta.
#
#    Supsta is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Supsta is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'supsta/gimp/integer'
require 'supsta/gimp/item'
require 'supsta/gimp/layer'

module Supsta
  module Gimp

    ##
    # Class representing a GIMP drawable.

    class Drawable < Item

      ##
      # Deletes this Drawable.

      def delete!
        return unless @self
        @app.script.concat "(gimp-drawable-delete #{@self})\n"
        @app = @self = nil

        nil
      end

      ##
      # Copies from this drawable to the clipboard.

      def copy!
        @app.script.concat "(gimp-edit-copy #{@self})\n"

        self
      end

      ##
      # Pastes from the clipboard and returns the resulting floating Layer.

      def paste
        var = @app.new_var
        scope.def var, "(car(gimp-edit-paste #{@self} 1))"

        Layer.new(@app, var, @image)
      end

      ##
      # Inverts this Drawable.

      def invert
        @app.script.concat "(gimp-invert #{@self})\n"

        self
      end

      ##
      # Clears this drawable.

      def clear
        @app.script.concat "(gimp-edit-clear #{@self})\n"

        self
      end

      ##
      # Fills this Drawable, using the specified color, or GIMP's foreground
      # color if none is specified.
      #
      # :call-seq: fill(r, g, b)
      #            fill()

      def fill(*rgb)
        @app.set_foreground(*rgb[0, 3]) unless rgb.empty?
        @app.script.concat "(gimp-edit-fill #{@self} 0)\n"

        self
      end

      ##
      # Returns this Drawable's height.

      def height
        var = @app.new_var
        scope.def var, "(car (gimp-drawable-height #{@self}))"

        Integer.new(@app, var)
      end

      ##
      # Returns this Drawable's width.

      def width
        var = @app.new_var
        scope.def var, "(car (gimp-drawable-width #{@self}))"

        Integer.new(@app, var)
      end

    end
  end
end
